package com.braincookie.plugins.jira.config;

import java.io.IOException;
import java.io.Writer;
import java.util.Map;

import com.atlassian.jira.project.Project;
import com.atlassian.plugin.web.model.WebPanel;
import com.atlassian.sal.api.pluginsettings.PluginSettings;
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;

public class SummaryPanel implements WebPanel {
	
	private final PluginSettingsFactory settingsFactory;
	
	public SummaryPanel(PluginSettingsFactory settingsFactory) {
		this.settingsFactory = settingsFactory;
	}

	@Override
	public String getHtml(Map<String, Object> context) {
		Project project = (Project) context.get("project");
		
		if (project == null) return "Could not get project";
		
    	PluginSettings settings = settingsFactory.createSettingsForKey(project.getKey());

    	@SuppressWarnings("unchecked")
		Map<String,String> leftFragments = (Map<String,String>) settings.get("LeftFragments");
		Map<String,String> rightFragments = (Map<String,String>) settings.get("RightFragments");
		
		StringBuilder html = new StringBuilder("<div><table>");

    	for (Map.Entry<String, String> entry : leftFragments.entrySet()) {
    		html.append("<tr><td>Left</td><td>["+entry.getKey()+"]</td>");
    	    html.append("<td>"+entry.getValue()+"</td></tr>");
    	}
		
    	for (Map.Entry<String, String> entry : rightFragments.entrySet()) {
    		html.append("<tr><td>Right</td><td>["+entry.getKey()+"]</td>");
    	    html.append("<td>"+entry.getValue()+"</td></tr>");
    	}
    	
		html.append("</table></div>");
		return html.toString();
	}

	@Override
	public void writeHtml(Writer writer, Map<String, Object> context)
			throws IOException {
		// TODO Auto-generated method stub

	}

}
